package mydessertstore.web.servlet;

import mydessertstore.domain.Account;
import mydessertstore.domain.Order;
import mydessertstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ShippingAddressServlet extends HttpServlet {

    private static final String CONFIRM_ORDER_FORM = "/WEB-INF/jsp/order/confirmOrder.jsp";

    private Account account;
    private Order order;
    private String shipToFirstName;
    private String shipToLastName;
    private String shipAddress1;
    private String shipAddress2;
    private String shipCity;
    private String shipState;
    private String shipZip;
    private String shipCountry;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset = UTF-8");

        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset = UTF-8");

        shipToFirstName = req.getParameter("shipToFirstName");
        shipToLastName = req.getParameter("shipToLastName");
        shipAddress1 = req.getParameter("shipAddress1");
        shipAddress2 = req.getParameter("shipAddress2");
        shipCity = req.getParameter("shipCity");
        shipState = req.getParameter("shipState");
        shipZip = req.getParameter("shipZip");
        shipCountry = req.getParameter("shipCountry");

        HttpSession session = req.getSession();
        order = (Order)session.getAttribute("order");

        order.setShipToFirstName(shipToFirstName);
        order.setShipToLastName(shipToLastName);
        order.setShipAddress1(shipAddress1);
        order.setShipAddress2(shipAddress2);
        order.setShipCity(shipCity);
        order.setShipState(shipState);
        order.setShipZip(shipZip);
        order.setCourier(shipCountry);

        session.setAttribute("order", order);

        account = (Account) session.getAttribute("account");
        //日志功能
        if(account!=null) {

            LogService logService = new LogService();

            String logInfo = logService.logInfo(" ") + " 修改邮寄地址 " ;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        else{
            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + " 修改邮寄地址 " ;
            logService.insertLogInfo("未登录用户", logInfo);
        }
        //日志结束

        req.getRequestDispatcher(CONFIRM_ORDER_FORM).forward(req, resp);
    }
}
